<?php
/**
 * Created by PhpStorm.
 * User: 王福
 * 定时任务
 * Date: 2018/12/13
 * Time: 10:47
 */

namespace app\index\controller;
use app\common\controller\Frontend;
use think\Db;
use QL\QueryList;
class Timingtask extends Frontend
{
    /**
     * adminwork任务操作
     */
    public function adminwork_option(){
        $linkWork = Db::name('adminwork');
        $workData = $linkWork->field('id,expire_date')->where(array('statuslist'=>array('IN','1,2,3')))->select();
        $time = time();
        foreach ($workData as $k=>$v){
            if($v['expire_date']&&$v['expire_date']!='0000-00-00 00:00:00'){
                //有时间限制的任务
                $expire_date = strtotime($v['expire_date']);
                if($expire_date<=$time){
                    //已逾期
                    $linkWork->where(array('id'=>$v['id']))->update(array('qilist'=>'2'));
                }
            }
        }
        echo '成功';
    }

    /**
     * 查询合作到期的平台
     */
    public function cooperation_option(){
        $linkCooperation = Db::name('Cooperation');
        $linkAllot = Db::name('Allot');
        $data = $linkCooperation->field('id,endtime,allot_id')->where(array('statuslist'=>'1'))->select();
        $time = time();
        foreach ($data as $k=>$v){
            if($v['endtime']&&$v['endtime']!='0000-00-00 00:00:00'){
                //存在过期时间
                $endtime = strtotime($v['endtime']);
                if($endtime&&$endtime<=$time){
                    $linkCooperation->where(array('id'=>$v['id']))->update(array('statuslist'=>'2'));
                    //查询当前合作人是否还有续费记录
                    $xuData = $linkCooperation->where(array('allot_id'=>$v['allot_id'],'statuslist'=>'1'))->find();
                    if(!$xuData){
                        //当前客户如果没有未到期的数据修改为已过期
                       $linkAllot->where(array('id'=>$v['allot_id']))->update(array('qilist'=>'2'));
                    }
                }
            }
        }
        echo '成功';
    }

    /**
     * 定时查询B端平台用户工商信息
     */
    public function buserinfo_option(){
        $linkBuser = Db::name('Buser');
        $linkInfo = Db::name('buserinfo');
        $buserData = $linkBuser->field('id,pinname')->select();
        foreach ($buserData as $k=>$v){
            if($v['pinname']){
                $url = 'http://www.dailuopan.com/p2p/'.$v['pinname'];
                //抓URL
                $rules = [
                    'url' => ['#goLink', 'text'],
                ];
                $urlArray = QueryList::get($url)->rules($rules)->query()->getData();
                $field['url'] = $urlArray[0]['url'];
                //组织架构
                $rules = [
                    'name'=>['.member-info  .list li', 'text','-.member-info  -.list -li -span'],
                    'val'=>['.member-info  .list li span', 'text'],
                ];
                $jiaData = QueryList::get($url)->rules($rules)->queryData();
                $field['organization'] = json_encode($jiaData);
                //权利地图
                // 采集表头
                $tableHeader = QueryList::get($url)->find('body > div.wrapper.detailContainer.clearfix > div:nth-child(11) > div.detailGongshang.clearfix > div.gudong-info.clearfix > dl > dt:eq(0)')->find('span')->texts();
                // 采集表的每行内容
                $tableRows = QueryList::get($url)->find('body > div.wrapper.detailContainer.clearfix > div:nth-child(11) > div.detailGongshang.clearfix > div.gudong-info.clearfix > dl > dd:EGT(0)')->map(function($row){
                    return $row->find('span')->texts()->all();
                });
                $quanArray = array();
                $quanArray['tableHeader'] = $tableHeader;
                $quanArray['tableRows'] = $tableRows;
                $field['right'] = json_encode($quanArray);
                //评级
                $rules = [
                    // 采集文章标题
                    'val' => ['body > div.wrapper.detailContainer.clearfix > div.detailRate.detailBox.clearfix.mt15 > div.rate-track.clearfix .rate-track-data .rate-info dt',
                        'text','-.iconfont -.bijiao'],
                ];
                $jiData = QueryList::get($url)->rules($rules)->query()->getData();
                $composite = array();
                foreach ($jiData as $ko=>$vo){
                    $val = '';
                    if($vo['val']){
                        $val = str_replace(array("\r\n", "\r", "\n","\t"), array(' ','','',''), $vo['val']);
                    }
                    $composite[] = array(
                        'val'=>$val
                    );
                }
                $field['composite'] = json_encode($composite);
                $field['buser_id'] = $buserData[0]['id'];
//工商信息
                $rules = [
                    'name'=>['.basic-info .tab:nth-child(1) .tit', 'text'],
                    'val'=>['.basic-info .tab:nth-child(1) .text', 'text'],
                ];
                $gongData = QueryList::get($url)->rules($rules)->queryData();
                $rules = [
                    'name'=>['.basic-info .tab:nth-child(2) .tit', 'text'],
                    'val'=>['.basic-info .tab:nth-child(2) td:nth-child(2)', 'text'],
                ];
                $qiData = QueryList::get($url)->rules($rules)->queryData();
                $circles = array_merge($gongData,$qiData);;
                $field['circles'] = json_encode($circles);
                //健康度指
                $rules = [
                    'key'=>['body > div.wrapper.detailContainer.clearfix > div:nth-child(10) > div.detailHealth.detailHFlow > div.right > div.info.clearfix > ul span.text',
                        'text','-i'],
                    'val'=>['body > div.wrapper.detailContainer.clearfix > div:nth-child(10) > div.detailHealth.detailHFlow > div.right > div.info.clearfix > ul span.text i',
                        'text','']
                ];
                $jianData = QueryList::get($url)->rules($rules)->query()->getData();
                $healthy = array();
                foreach ($jianData as $ko=>$vo){
                    $name = '';
                    if($vo['key']){
                        $name = rtrim($vo['key'],' |');
                    }

                    $healthy[] = array('key'=>$name,'val'=>$vo['val']);
                }
                $field['healthy'] = json_encode($healthy);
                $infoData = $linkInfo->where(array('buser_id'=>$v['id']))->count();
                if($infoData){
                    $linkInfo->where(array('buser_id'=>$v['id']))->update($field);
                }else{
                    $linkInfo->insertGetId($field);
                }
                echo '成功';
            }
        }
    }
}